<template>
	<view class="uni-common-mt">
    <!-- 酒店信息  -->
    <view class="hotel-info-card uni-common-mt" v-if="hotel != null">
    	<!-- 名称 -->
    	<view class="hotel-info-card__title">
    		<text class="font-bold">{{ hotel.hotelName }}</text>
    	</view>
    	<!-- 评分 -->
    	<view class="flex justify-between margin-bottom margin-top-xs">
    		<buk-rate-capsule text="真棒👍🏻" :rate="` ` + hotel.rstar" />
    	</view>
    </view>
		<!-- 房型信息 -->
		<uni-section title="房型信息" type="line" class="margin-top-xs">
			<view class="container-grid justify-center align-center">
				<view class="margin-left">
					<text class="text-primary font36 font-bold">{{ ratePlan.roomTypeName }}</text>
				</view>
				<view>
					<text class="" v-if="!ratePlan.cancelPolicies[0].canCancel">不可取消</text>
				</view>
				<view>
					<text class="font24" v-if="ratePlan.rates[0].mealCount == 0">无早</text>
				</view>
			</view>
		</uni-section>
		<!-- 入住信息 -->
		<uni-section title="入住信息" type="line" class="margin-top-xs">
			<view class="container-grid-col-2 justify-center align-center">
				<view class="margin-left margin-bottom">
					<text>入住日期</text>
				</view>
				<view class="margin-bottom">
					<text class="font-bold font24">{{ orderInfo.checkInDate }}</text>
				</view>
				<view class="margin-left margin-bottom">
					<text>离店日期</text>
				</view>
				<view class="margin-bottom">
					<text class="font-bold font24">{{ orderInfo.checkOutDate }}</text>
				</view>
				<view class="margin-left">
					<text>房间数</text>
				</view>
				<view>
					<uni-number-box v-model="orderInfo.roomCount" @change="changeRoomCount" :min="1" />
				</view>
			</view>
		</uni-section>
		<uni-section title="住客信息" type="line" class="margin-top-xs">
			<template v-for="(psg, index) in passengers" :key="index">
				<view class="container-grid-col-2 margin align-center">
					<view class="margin-bottom justify-end align-end">
						<text>姓名 {{ (index+1) }}</text>
					</view>
					<view class="margin-bottom">
						<uni-easyinput placeholder="请输入姓名" v-model="psg.name" />
					</view>
				</view>
			</template>
		</uni-section>
		<uni-section title="联系信息" type="line" class="margin-top-xs">
			<view class="container-grid-col-2  margin align-center">
				<view class="margin-bottom justify-end align-end">
					联系人
				</view>
				<view class="margin-bottom">
					<uni-easyinput placeholder="请输入姓名" v-model="linkman" />
				</view>
			</view>
		</uni-section>
    <!-- 房价信息 -->
    <uni-section title="房价信息" type="line" class="margin-top-xs">
    	<view class="container-grid justify-center align-center">
    		<view class="pay-btn margin-left" v-for="(rate, index) in ratePlan.rates" :key="index">
    			<view class="justify-center">{{ rate.startDate }}</view>
    			<view class="pay-btn__bottom uni-warning font36 font-bold">{{ rate.inclusiveAmount }}</view>
    		</view>
    	</view>
    </uni-section>
		<view class="justify-center align-center flex margin-top-xs">
			<button type="primary"  @click="createOrder" :loading="is_creating">提交订单</button>
		</view>
	</view>
</template>

<script setup>
	import {
		ref,
		reactive
	} from 'vue'
	import {
		onShow,
		onLoad
	} from '@dcloudio/uni-app'
	import {
		useHotelStore
	} from '@/store/hotel.js'
	import {
		createHotelOrder
	} from '@/common/api/hotel.js'

	const hotelStore = useHotelStore()

	let hotel = ref({})
	let orderInfo = ref({})
	let ratePlan = ref({})
	let selectedRoomType = ref('')
	let roomCount = ref(1)
	let guestCount = ref(1)

	let linkman = ref('测试联系人')
	let contactPhone = ref('')
	let passengers = ref([{
		name: '张三',
		mobile: ''
	}])

	onLoad(() => {
		console.log('on load')
		try {
			if (hotelStore.ratePlan.hotelId === undefined) {
				let jsonString = uni.getStorageSync('hotel_rate_plan')
				console.log(jsonString)
				let obj = JSON.parse(jsonString)
				hotelStore.updateRatePlan(obj)
			}
			if (hotelStore.hotelInfo.id === undefined) {
				let jsonString = uni.getStorageSync('hotel_info')
				console.log(jsonString)
				let obj = JSON.parse(jsonString)
				hotelStore.updateHotelInfo(obj)
			}
			if (hotelStore.orderInfo.hotelId === undefined) {
				let jsonString = uni.getStorageSync('hotel_order_info')
				console.log(jsonString)
				let obj = JSON.parse(jsonString)
				hotelStore.updateOrderInfo(obj)
			}
			hotel.value = hotelStore.hotelInfo
			orderInfo.value = hotelStore.orderInfo
			ratePlan.value = hotelStore.ratePlan
			console.log(hotel.value)
			console.log(orderInfo.value)
			console.log(ratePlan.value)
			if (orderInfo.value.roomCount < 1) {
				orderInfo.value.roomCount = 1
			}
		} catch (err) {
			console.log(err)
		}
	})

	function changeRoomCount(e) {
		// console.log(orderInfo.value.roomCount)
		while (orderInfo.value.roomCount > passengers.value.length) {
			passengers.value.push({
				name: '',
				mobile: ''
			})
		}

		if (orderInfo.value.roomCount < passengers.value.length) {
			passengers.value.splice(orderInfo.value.roomCount)
		}
	}


	let is_creating = ref(false)

	function createOrder() {
		// 生成订单逻辑
		let prices = [];
		console.log(prices)
		console.log(ratePlan.value.rates)
		for (let rate of ratePlan.value.rates) {
			console.log(rate)
			prices.push({
				"currency": "RMB",
				"discount": rate.discount,
				"effectiveDate": rate.startDate,
				"exclusiveAmount": rate.exclusiveAmount,
				"inclusiveAmount": rate.inclusiveAmount,
				"instantConfirm": rate.instantConfirm,
				"mealCount": rate.mealCount,
				"mealType": rate.mealType,
				"serviceCharge": rate.serviceCharge
			})
		}

		is_creating.value = true
		const params = {
			hotelName: hotel.value.hotelName,
			hotelId: hotel.value.id,
			checkInDate: orderInfo.value.checkInDate,
			checkOutDate: orderInfo.value.checkOutDate,
			roomCount: orderInfo.value.roomCount,
			roomId: orderInfo.value.roomId,
			roomTypeId: orderInfo.value.roomTypeId,
			roomTypeName: ratePlan.value.roomTypeName,
			roomType: ratePlan.value.roomTypeName,
			linkman: linkman.value,
			contactPhone: contactPhone.value,
			prices: prices,
			passengers: passengers.value,
			payType: "8",
			totalAmount: orderInfo.value.price
		};

		console.log(params)
		try {

			createHotelOrder(
				params,
				(v) => {
					console.log(v)
					if (v.errcode !== 0) {
						console.log(v.errmsg)
						uni.showToast({
							title: '操作失败: ' + v.errmsg,
							duration: 3000,
							icon: 'none'
						})
					} else {
						uni.showToast({
							title: '提交成功'
						})
						uni.redirectTo({
							url: '/pages/hotel/HotelOrders'
						})
					}
				},
				() => {
					is_creating.value = false
				}
			);
		} catch (err) {
			console.log(err)
		}
	}
</script>



<style lang="scss">
	.container-grid {
		display: grid;
		// height: 150rpx;
		grid-template-columns: 1fr 1fr 1fr;
	}

	.container-grid-col-2 {
		display: grid;
		// height: 150rpx;
		grid-template-columns: 3fr 7fr;
	}

	.pay-btn {
		background: $uni-bg-color-grey;
		padding: 4rpx;
		// color: #ffffff;
		font-size: 24rpx;
		// min-width: 120rpx;
		text-align: center;

		&__bottom {
			background-color: #ffffff;
			width: 100%;
			// color: #242424;
			font-size: 28rpx;
			text-align: center;
		}

		&--hover {
			opacity: 0.8;
		}
	}

	.uni-list-item__extra {
		// width: 25%;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: row;
		justify-content: flex-end;
		align-items: center;
	}


	.uni-list-item__extra-text {
		color: $uni-text-color-grey;
		font-size: $uni-font-size-sm;
	}

	.uni-list-item__content {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		padding-right: 8px;
		flex: 1;
		color: #3b4144;
		// overflow: hidden;
		flex-direction: column;
		justify-content: space-between;
		overflow: hidden;
	}

	.uni-list-item__content-title {
		font-size: $uni-font-size-base;
		color: #3b4144;
		overflow: hidden;
	}

	.hotel-banner {
		height: 580rpx;
	}

	.hotel-info-card {
		position: relative;
		top: -60rpx;
		padding: 32rpx;
		// height: 488rpx;
		background: #ffffff;
		border-radius: 50rpx 50rpx 20rpx 20rpx;
		margin-bottom: -60rpx;

		&__tags {
			white-space: nowrap;
			width: 500rpx;
			overflow: hidden;
		}

		&__title {
			font-size: 36rpx;
			font-weight: 400;
			line-height: 50rpx;
			color: #242424;
			display: flex;
			flex-wrap: wrap;
			align-items: center;
		}

		&__map {
			padding: 24rpx;
			background: #f8f8f8;
			border-radius: 20rpx 20rpx 20rpx 20rpx;

			&__title {
				width: 466rpx;
				font-size: 28rpx;
				font-weight: 400;
				color: #242424;
				line-height: 39rpx;
			}
		}
	}

	.indicator-num {
		padding: 2px 0;
		background-color: rgba(0, 0, 0, 0.35);
		border-radius: 100px;
		width: 35px;
		justify-content: center;

		&__text {
			color: #ffffff;
			font-size: 12px;
		}
	}

	.user-choose-time {
		margin: 24rpx 0;
		padding: 24rpx 32rpx;
		background: #242424;
		color: #ffffff;
	}

	.hotel-room-type {
		padding: 0 32rpx;
		display: flex;
		flex-direction: column;
	}
</style>